#include "../include/head.h"

int main() {
    // 初始化链表
    Pnode_list goodsList = creat_list();
    read_file(goodsList);
    PUserNode userList = user_creat_list();
    user_read_file(userList);
    
    // 登录与注册
    PUserNode currentUser = NULL;
    while(currentUser == NULL) {
        system("clear");
        printf("------------------------欢迎使用商品管理系统--------------------------\n");
        printf("1. 登录\n");
        printf("2. 注册\n");
        printf("请选择操作: ");
        int choice;
        scanf("%d", &choice);
        getchar();
        system("clear");
        switch(choice) {
            case 1:
                currentUser=user_login(userList);
                printf("按回车键继续\n");
                scanf("%*c");
                break;
            case 2:
                users_register(userList);
                printf("按回车键继续\n");
                scanf("%*c");
                break;
            default:
                printf("无效选择\n");
        }
    }

    // 主菜单循环
    while(1) {
        system("clear");
        dispmenue();
        printf("请选择操作: ");
        int choice;
        scanf("%d", &choice);
        getchar();
        system("clear");
        switch(choice) {
            case 1: // 商品管理
                if(currentUser->info.user_type == USER_TYPE_ADMIN) {
                    dispmenu1_1(goodsList);
                } else {
                    printf("您的权限不够\n");
                    printf("按回车键继续\n");
                    scanf("%*c");
                }
                break;
            case 2: // 库存管理
                dispmenu2(goodsList, currentUser->info.user_type);
                break;
            case 3: // 用户管理
                dispmenue3(currentUser,userList);
                break;
            case 4: // 退出
                free(goodsList);
                free(userList);
                return 0;
            default:
                printf("无效选择\n");
        }
    }
}